package com.ruoyi.common.log.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.log.config.MinioUpload;
import com.ruoyi.common.log.domain.*;
import com.ruoyi.common.log.domain.Process;
import com.ruoyi.common.log.service.*;
import com.ruoyi.common.log.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/process")
public class ProcessController {

    @Autowired
    ProcessService processService;
    @Autowired
    CityService cityService;
    @Autowired
    CropService cropService;
    @Autowired
    CargoService cargoService;
    @Autowired
    RetailService retailService;


    /**
     * 列表查询
     * 分页
     * @param pageNum
     * @return
     */
    @RequestMapping("/list")
    public Result list(@RequestBody Process process, @RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "3") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Process> list = processService.listAll(process);
        return Result.success(new PageInfo<>(list));
    }

    /**
     * 省市区三级联动
     * @return
     */
    @RequestMapping("/gettype")
    public Result gettype() {
        List<City> list = cityService.list();
        //配置'
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都有默认值的
        treeNodeConfig.setIdKey("value");
        treeNodeConfig.setNameKey("label");
        treeNodeConfig.setDeep(3);
        treeNodeConfig.setParentIdKey("pid");
        treeNodeConfig.setChildrenKey("children");
        // 最大递归深度
        //转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<Object>> treeNodes = TreeUtil.<City, Object>build(list, 1, treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getCityId());
                    tree.setParentId(treeNode.getPid());
                    tree.setName(treeNode.getCityname());
                });
        return Result.success(treeNodes);
    }

    /**
     * 状态下拉框查询
     * @return
     */
    @RequestMapping("/getList")
    public Result getList(){
        List<Process> list = processService.list();
        return Result.success(list);
    }

    /**
     * 添加加工或修改加工
     * @param process
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Process process){
        boolean save = processService.saveOrUpdate(process);
        return save?Result.success():Result.fail();
    }

    /**
     * 查询农作物 下拉框
     * @return
     */
    @RequestMapping("/getCrop")
    public Result getCrop(){
        List<Crop> list = cropService.list();
        return Result.success(list);
    }

    /**
     * 查询仓库 下拉框
     * @return
     */
    @RequestMapping("/getCargo")
    public Result getCargo(){
        List<Cargo> list = cargoService.list();
        return Result.success(list);
    }

    /**
     * 查询零售商 下拉框
     * @return
     */
    @RequestMapping("/getRetail")
    public Result getRetail(){
        List<Retail> list = retailService.list();
        return Result.success(list);
    }

    /**
     * 三级联动回显
     * @param processId
     * @return
     */
    @RequestMapping("/search")
    public Result search(Integer processId){
        Process process=processService.search(processId);
        return Result.success(process);
    }

    /**
     * 根据id删除
     * @param processId
     * @return
     */
    @RequestMapping("/delById")
    public Result delById(Integer processId){
        boolean b = processService.removeById(processId);
        return b?Result.success():Result.fail();
    }

    /**
     * 图片上传
     * @param file
     * @return
     */
    @RequestMapping("/addPic")
    public Result addPic(MultipartFile file){
        String s = MinioUpload.uploadPic(file);
        return Result.success(s);
    }

    @RequestMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
        String fileName = URLEncoder.encode("加工厂信息", "UTF-8").replaceAll("\\+", "%20");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
        List<Process> list = processService.list2();
        EasyExcel.write(response.getOutputStream(), Process.class).sheet("加工厂表格").doWrite(list);
    }

}
